package com.quwan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.app.vo.MeFollowersDTO;
import com.quwan.app.vo.MemberDashboardData;
import com.quwan.entity.TMemberFollow;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/**
 * @author Hayson
 * @since 2021/7/5 23:44
 */
public interface TMemberFollowMapper extends BaseMapper<TMemberFollow> {
    @Results({
            @Result(column = "follower", property = "follower", javaType = Integer.class),
            @Result(column = "favorites", property = "favorites", javaType = Integer.class),
            @Result(column = "visitor", property = "visitor", javaType = Integer.class),
            @Result(column = "vip", property = "vip", javaType = Boolean.class, jdbcType = JdbcType.INTEGER)
    })
    @Select("select " +
            "(select count(1) c from t_member_follow where target_member = #{mId} and  status =1) follower," +
            "(select count(1) c from t_member_follow where follow_member = #{mId} and  status =1) favorites," +
            "(select count(1) c from t_member_visit  where target_member = #{mId}) visitor," +
            "(select count(1) c from t_member_vip where m_id = #{mId} and end_time > now()) vip")
    MemberDashboardData selectDashboardDataByMId(Long mId);


    @Select("select " +
            "(select count(1) c from t_member_follow where un_lock =#{unLock} and  status =1) follow," +
            "(select count(1) c from t_member_black_list where un_lock=#{unLock}  and status =1) black ")
    Map<String, Long> followAndBlackList(String unLock);

    @Insert("insert into t_member_follow(follow_member,target_member,status,un_lock)" +
            "values (#{followMember} ,#{targetMember} ,#{status} ,#{unLock})" +
            "ON duplicate key update status =#{status} ")
    Integer findAndSetFollow(TMemberFollow tMemberFollow);

    @Select("select " +
            "count(id)follow_member ,target_member from t_member_follow where target_member in(${ids}) and status =1 group  by target_member")
    List<TMemberFollow> selectFensi(String ids);

    @Select("select " +
            "count(id) follow_member ,un_lock from t_member_follow where un_lock in(${ids}) and status =1 group  by un_lock")
    List<TMemberFollow> selectIsFollow(String followIds);

    @Select("select " +
            "m.nick_name,m.img_url image_url,m.id m_id," +
            "(select count(1) from t_member_follow where un_lock =concat(m.id,${mId}) and status =1 ) tips " +
            "from t_member m  where exists (select 1 from t_member_follow f  " +
            "where f.follow_member =#{mId} and f.status= 1 and f.target_member = m.id )")
    Page<MeFollowersDTO> selectMyFollowDetails(Page<Object> objectPage, @Param("mId") String id);

    @Select("select " +
            " m.nick_name,m.img_url image_url,m.id m_id," +
            "(select count(1) from t_member_follow where follow_member = #{mId} and target_member=m.id and  status =1) as follow " +
            " from t_member m " +
            "where exists (select 1 from t_member_follow f where f.target_member =#{mId} and status = 1  and f.follow_member =m.id) order by m.auto_id desc")
    Page<MeFollowersDTO> selectMyFollowers(Page<Object> objectPage,@Param("mId") String id);
}